﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;

namespace 数据函数库
{
    /// <summary>
    /// 包头
    /// </summary>
    public class TcpPacketHead
    {
        /// <summary>
        /// 包头大小
        /// </summary>
        public static Int32 HEAD_SIZE = 7;
        public UInt16 GreenhouseAddr;
        public byte NoteNum;
        public byte Command;
        public byte Len = 0;
        public byte[] Data;
        public TcpPacketHead()
        {
            Data = new byte[HEAD_SIZE];
        }
    }
    public class TcpPacket
    {
        public TcpPacketHead Header;
        public byte[] Data;
        public byte[] AllData;
        public int RxCount;
        public byte[] RxList;
        public static Int32 BUFFER_SIZE = 1024;
//         public TcpPacket(byte com, UInt16 _GreenhouseAddr, byte NoteNum)
//         {
//             NeedCommand = com;
//             GreenhouseAddr = _GreenhouseAddr;
//             Header = new TcpPacketHead();
//             RxCount = 0;
//         }
        public TcpPacket()
        {
            Header = new TcpPacketHead();
            RxCount = 0;
        }
        public static byte[] MakeTcpPacket(byte com, UInt16 _GreenhouseAddr, byte _NoteNum, byte b)
        {
            return MakeTcpPacket(com, _GreenhouseAddr,_NoteNum, new byte[] { b });
        }
        public static byte[] MakeTcpPacket(byte com, UInt16 _GreenhouseAddr,byte _NoteNum, byte[] data)
        {
            byte[] txbuffer = new byte[TcpPacketHead.HEAD_SIZE + data.Length + 2];
            txbuffer[0] = 0x16;
            txbuffer[1] = 0x16;
            txbuffer[2] = BytesOP.GetHighByte(_GreenhouseAddr);
            txbuffer[3] = BytesOP.GetLowByte(_GreenhouseAddr);
            txbuffer[4] = _NoteNum;
            txbuffer[5] = com;
            txbuffer[6] = (byte)data.Length;
            for (int i = 0; i < data.Length; i++)
            {
                txbuffer[i + TcpPacketHead.HEAD_SIZE] = data[i];
            }
            txbuffer[TcpPacketHead.HEAD_SIZE + data.Length] = Verify.GetVerify_byteSum(txbuffer, TcpPacketHead.HEAD_SIZE + data.Length);
            txbuffer[TcpPacketHead.HEAD_SIZE + data.Length + 1] = 0x0d;
            return txbuffer;
        }
        public static object SaveDataProcessCallbackProc(ref byte[] tempbuffer, ref byte[] buffer, ref int dataOffset, int length)
        {
            //判断读取的字节数+缓冲区已有字节数是否超过缓冲区总大小
            int index=0;
            TcpPacket[] ret = null;
            while(index<length)
            {
                buffer[dataOffset ++] = tempbuffer[index++];
                if (dataOffset == 1)
                {
                    if (buffer[0] != 0x16)
                        dataOffset = 0;
                }
                else if (dataOffset == 2)
                {
                    if (buffer[0] != 0x16)
                        dataOffset = 0;
                }
                else if (dataOffset > 7)//已经收到了数据长度
                {
                    if (dataOffset == buffer[6] + TcpPacketHead.HEAD_SIZE + 2)//数据接收完成
                    {
                        if (buffer[dataOffset - 1] == 0x0d)
                        {
                            byte s1 = Verify.GetVerify_byteSum(buffer,dataOffset-2);
                            //if (s1 == buffer[dataOffset - 2])
                            {
                                if (ret == null)
                                    ret = new TcpPacket[1];
                                else
                                    Array.Resize<TcpPacket>(ref ret, ret.Length+1);
                                ret[ret.Length-1] = new TcpPacket();
                                Array.Copy(buffer, 0, ret[ret.Length - 1].Header.Data, 0, 7);
                                ret[ret.Length - 1].Header.GreenhouseAddr = BytesOP.MakeShort(buffer[2], buffer[3]);
                                ret[ret.Length - 1].Header.NoteNum = buffer[4];
                                ret[ret.Length - 1].Header.Command = buffer[5];
                                ret[ret.Length - 1].Header.Len = buffer[6];
                                ret[ret.Length - 1].Data = new byte[ret[ret.Length - 1].Header.Len];
                                Array.Copy(buffer, 7, ret[ret.Length - 1].Data, 0, ret[ret.Length - 1].Header.Len);
                                ret[ret.Length - 1].AllData = new byte[dataOffset];
                                Array.Copy(buffer, 0, ret[ret.Length - 1].AllData, 0, dataOffset);
                                dataOffset = 0;
                            }
//                             else
//                             {
//                                 dataOffset = 0;
//                             }
                        }
                        else
                        {
                            dataOffset = 0;
                        }
                    }
                }
            }           
            return ret;
        }
        
    }
}
